package com.xhwl.data.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xhwl.common.pojo.cent.sdata.Industry;
import com.xhwl.data.dao.IndustryDao;
import com.xhwl.data.pojo.vo.IndustryVO;
import com.xhwl.data.service.IIndustryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestPart;

import java.util.List;

/**
 * @description:
 * @author:
 * @create: 2020-11-25
 **/
@Service
public class IndustryServiceImpl extends ServiceImpl<IndustryDao, Industry> implements IIndustryService {


    @Autowired
    IndustryDao industryDao;

    private static final Logger log = LoggerFactory.getLogger(IndustryServiceImpl.class);

    @Override
    public Integer findIdByType(String industryType) {
        QueryWrapper<Industry> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type", industryType);
        Industry industry = industryDao.selectOne(queryWrapper);

        return industry.getId();

    }

    @Override
    public List<Industry> getAll() {
        return industryDao.selectList(new QueryWrapper<>());
    }

    @Override
    public Industry getByCode(String code) {
        if(StringUtils.isEmpty(code)){
            return null;
        }
        QueryWrapper<Industry> objectQueryWrapper = new QueryWrapper<>();
        List<Industry> industries = industryDao.selectList(objectQueryWrapper);
        if(CollectionUtils.isEmpty(industries)){
            return null;
        }else if(industries.size() > 1){
            log.error("查询业态类型错误，查到了多个 {}", code);
        }
        return industries.get(0);
    }

    @Override
    public Industry getById(Integer id) {
        if (StringUtils.isEmpty(id)) return null;
        return industryDao.selectById(id);
    }

    @Override
    public List<Industry> list(IndustryVO industryVO) {
        QueryWrapper<Industry> industryQueryWrapper = new QueryWrapper<>();
        if (null != industryVO.getKeywords() && industryVO.getKeywords().trim().length() > 0) {
            industryQueryWrapper.like("name", industryVO.getKeywords()).or().like("type", industryVO.getKeywords());
        }
        if (null != industryVO.getName() && industryVO.getName().trim().length() > 0) {
            industryQueryWrapper.like("name", industryVO.getName());
        }
        if (!CollectionUtils.isEmpty(industryVO.getIds())) {
            industryQueryWrapper.in("id", industryVO.getIds());
        }
        List<Industry> industries = industryDao.selectList(industryQueryWrapper);
        return industries;
    }

}
